//
//  WLStatusPictureView.swift
//  WLWeibo
//
//  Created by Devil on 16/8/14.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

private let WLStatusPictureViewCellId = "WLStatusPictureViewCellId"
private let margin: CGFloat = 5
private let CellWH: CGFloat = 80

class WLStatusPictureView: UICollectionView {

    var pictureInfos:[WLPictureInfo]?{
    
        didSet{
        
//            countLabel.text = "\(pictureInfos?.count ?? 0)"
            
            let size = getPictureViewSize(pictureInfos?.count ?? 0)
            
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            
            reloadData()
            
        
        }
    
    }
    
    func getPictureViewSize(count: Int)-> CGSize{
    
        //行数，列数
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        //宽高
        let w = CGFloat(col) * CellWH + CGFloat(col - 1)*margin
        let h = CGFloat(row) * CellWH + CGFloat(row - 1)*margin
        
        return CGSize(width: w, height: h)
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        backgroundColor = RandomColor()
        addSubview(countLabel)
        
        countLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        registerClass(WLStatusPictureViewCell.self, forCellWithReuseIdentifier: WLStatusPictureViewCellId)
        
        dataSource = self
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: CellWH, height: CellWH)
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
        
    
    }
    
    

    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35 )
    

}

extension WLStatusPictureView: UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfos?.count ?? 0
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WLStatusPictureViewCellId, forIndexPath: indexPath) as! WLStatusPictureViewCell
        cell.picUrl = pictureInfos![indexPath.item]
        return cell
    }


}

class WLStatusPictureViewCell: UICollectionViewCell {
    
    var picUrl: WLPictureInfo?{
    
        didSet{
            bgImgView.wl_setImageWithString(picUrl?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif") {
                
                gifImgView.hidden = false
            }else{
                gifImgView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
    
        backgroundColor = RandomColor()
        contentView.addSubview(bgImgView)
        contentView.addSubview(gifImgView)
        
        bgImgView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImgView.snp_makeConstraints { (make) in
            make.bottom.trailing.equalTo(contentView)
        }
    
    
    }
    
    private lazy var bgImgView: UIImageView = {
        
        let img = UIImageView(imgName:"weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
     
    }()
    
    private lazy var gifImgView: UIImageView = UIImageView(imgName: "timeline_image_gif")
}

